﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.Meta.Quote.Data
{
    public class BarCompression : IComparable
    {
        private BarCompressionEnum _eBarCompression;
        private int _iInterval;

        public BarCompression(BarCompressionEnum eBarCompression) : this(eBarCompression, 0) { }

        public BarCompression(BarCompressionEnum eBarCompression, int iInterval)
        {
            switch (eBarCompression)
            {
                case BarCompressionEnum.Min_1:
                    _eBarCompression = BarCompressionEnum.Min;
                    _iInterval = iInterval != 1 ? iInterval : 1;
                    break;
                case BarCompressionEnum.Min_5:
                    _eBarCompression = BarCompressionEnum.Min;
                    _iInterval = 5;
                    break;
                default:
                    _eBarCompression = eBarCompression;
                    _iInterval = iInterval;
                    break;
            }
        }

        public BarCompressionEnum DataCompression
        {
            get
            {
                return _eBarCompression;
            }
        }

        public int Interval 
        {
            get
            {
                return _iInterval;
            }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            try
            {
                BarCompression barCompare = (BarCompression)obj;

                return ((int)DataCompression) * Interval == ((int)barCompare.DataCompression) * barCompare.Interval
                        ? 0
                        : ((int)DataCompression) * Interval > ((int)barCompare.DataCompression) * barCompare.Interval
                            ? 1
                            : -1;

            }
            catch { }
            return 0;
        }


        public override bool Equals(object obj)
        {
            return ((int)((BarCompression)obj).DataCompression == (int)DataCompression) &&
                   (((BarCompression)obj).Interval == Interval);
        }

        #endregion
    }
}
